package com.project.dao.impl;

import java.lang.reflect.Field;
import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title: BaseDao
 * @Package: com.project.dao.impl
 * @Description:
 * @Author: Liang-Xie
 * @Date: 2022/7/12 - 13:57
 */
@SuppressWarnings("all")
public class BaseDao {
    protected Connection con;
    protected PreparedStatement ps;
    protected ResultSet rs;

    public void setConnection() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?characterEncoding=utf-8", "root", "123456");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setClose() {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 更新数据库的数据
     * @param sql 执行的sql语句
     * @param valueArray 值列表
     */
    public void updateData(String sql,Object...valueArray){
        this.setConnection();
        try {
            ps=con.prepareStatement(sql);
            for (int i=0;i<valueArray.length;i++){
                ps.setObject(i+1,valueArray[i]);
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            setClose();
        }
    }

    public List find(String sql,Class beanClass,Object...valueArray){
        List list=new ArrayList<>();
        this.setConnection();
        try {
            ps=con.prepareStatement(sql);
            for (int i=0;i<valueArray.length;i++){
                ps.setObject(i+1,valueArray[i]);

            }
            rs=ps.executeQuery();
//            while (rs.next()){
////                调用实体类的无参构造方法产生实对象
//                Object obj=beanClass.getConstructor().newInstance();
////                得到实体类中所有属性列表
//                Field[]fArray=beanClass.getDeclaredFields();
//                for (Field f:fArray){
////                    从结果中得到当前值的属性
//                    Object value=rs.getObject(f.getName());
//                    if (f.getType()== LocalDate.class){
//                        value=LocalDate.parse(value.toString());
//                    }
////                    去掉修饰符检查
//                    f.setAccessible(true);
////                    将obj对象的f属性值，设置为value值
//                    f.set(obj,value);
//                }
//                list.add(obj);
//            }
            ResultSetMetaData rm=rs.getMetaData();
            int columCount=rm.getColumnCount();
            while (rs.next()){
                Object obj=beanClass.getConstructor().newInstance();
                for (int i=1;i<=columCount;i++){
//                    的到指定位置列名
                    String columName=rm.getColumnName(i);
//                    根据列名，得到结果集的值
                    Object value=rs.getObject(columName);
                    if (value instanceof java.sql.Date){
                        value=LocalDate.parse(value.toString());
                    }
//                    根据属性名或的对象
                    Field f=beanClass.getDeclaredField(columName);
                    f.setAccessible(true);
//                    给对象中的指定值赋值
                    f.set(obj,value);
                }
                list.add(obj);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            setClose();
        }
        return list;
    }


}
